/* 
 *   Date       Developer   Description
 *   
 *
 *   Classname:  zzz.test.component.mock.Test05
 * 
 *   Date:   2014-3-8
 * 
 *   The source code contained in this listing is proprietary to
 *   HLJ POST YITONG INFO-NET CO.,LTD.
 * 
 *   Unauthorized copying, adaptation, distribution, use, or display
 *   is strictly prohibited.
 *   This software is Copyright 2014 HLJ POST YITONG INFO-NET CO.,LTD.
 */
package com.titan.demo.camel.sample.component.mock;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.DefaultCamelContext;
import org.junit.Assert;

import com.titan.demo.camel.utils.CamelUtils;


/**
 *
 *
 * @author Administrator
 *
 */
public class Test05 {

    public static void main(String[] args) {
        CamelContext context = null;
        ProducerTemplate template = null;
        try {
            context = new DefaultCamelContext();
            context.addRoutes(new RouteBuilder() {
                public void configure() {
                    from("direct:a").to("mock:result");
                }
            });
            
            template = context.createProducerTemplate();
            template.start();
            context.start();
            
            MockEndpoint mock = context.getEndpoint("mock:result", MockEndpoint.class);
            mock.whenAnyExchangeReceived(new Processor() {
                public void process(Exchange exchange) throws Exception {
                    exchange.setException(new IllegalArgumentException("Forced"));
                }
            });

            template.sendBody("direct:a", "Hello World");
            Assert.fail("Should have thrown an exception");
            
        } catch (Exception e) {
            // 注意看异常类型和异常信息。
            e.printStackTrace();
        } finally {
            CamelUtils.close(template);
            CamelUtils.close(context);
        }
    }

}

